//
//  CZStatusPictureView.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
private let CZStatusPictureViewCellId = "CZStatusPictureViewCellId"
class CZStatusPictureView: UICollectionView, UICollectionViewDataSource {
	///每项之间的间距
	let itemMargin: CGFloat = 5
	///计算每项图片的宽度
	let itemWidth = (ScreenWidth - (2 * 10) - (2 * 5)) / 3
	
	///绑定数据模型
	var picUrls: [CZStatusPictureInfo]? {
		didSet{
		messageLabel.text = "\(picUrls?.count ?? 0)"
			let currenttSIze = calcSize(picUrls?.count ?? 0)
			
			
			//根据配图生成一个 cgsize,设置大小约束
			self.snp_updateConstraints { (make) in
				make.size.equalTo(currenttSIze)
				
			}
			reloadData()
		}
	}
	///懒加载
	private lazy var messageLabel: UILabel = UILabel(textColor: UIColor.redColor(), fontSIze: 20)
	
	override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
		let flowLayout = UICollectionViewFlowLayout()
		flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
		//设置间距垂直
		flowLayout.minimumLineSpacing = itemMargin
		//设置水平间距
		flowLayout.minimumInteritemSpacing = itemMargin
		
		super.init(frame: frame, collectionViewLayout: flowLayout)
		
		setUpUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	private func setUpUI() {
	
//	backgroundColor = RandomColor()
	addSubview(messageLabel)
		messageLabel.snp_makeConstraints { (make) in
			make.center.equalTo(self)
			
		}
		//注册 cell
		registerClass(CZStatusPictureViewCell.self, forCellWithReuseIdentifier: CZStatusPictureViewCellId)
		//设置数据源代理
		dataSource = self
	}
	///根据图片数量生成一个 CGSize
	private func calcSize(count: Int) ->CGSize {

	
	///列数
		let cols = count > 3 ? 3 : count
		
	///行数,记住公式
		let rows = (count - 1) / 3 + 1
		//当前视图的宽度
		let currentWidth = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
		//当前视图的高度
		let currentHeight = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
		
		return CGSize(width: currentWidth, height: currentHeight)
	}
	
	// MARK: ------UICollectionDataSource 数据源代理方法------
	
	func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
		return picUrls?.count ?? 0
	}
	
	func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
		let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZStatusPictureViewCellId, forIndexPath: indexPath) as! CZStatusPictureViewCell
//		cell.backgroundColor = RandomColor()
		//设置数据
		cell.picInfo = picUrls![indexPath.item]
		return cell
	}
	
}
//自定义配图 cell
class CZStatusPictureViewCell: UICollectionViewCell {
	//设置模型
	
	var picInfo: CZStatusPictureInfo? {
		didSet{
			guard let url = picInfo?.thumbnail_pic else {
			
			return
			}
			
			
    imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
//			
//			if url.hasSuffix(".gif") {
//    gifImageView.hidden = false
//			} else {
//			gifImageView.hidden = true
//			
//			}
			gifImageView.hidden = !url.hasSuffix(".gif")
		
		}
	}
	private lazy var imageView: UIImageView = {
		let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
		//设置图片等比填充
		view.contentMode = UIViewContentMode.ScaleAspectFill
		///多余部分截取掉
		view.clipsToBounds = true
	return view
	}()
	//gif 图片
	private lazy var  gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
	override	init(frame: CGRect) {
		super.init(frame: frame)
		setUpUI()
	}
	
	
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	//添加控件,设置约束
	private func setUpUI() {
	contentView.addSubview(imageView)
		contentView.addSubview(gifImageView)
		
	imageView.snp_makeConstraints { (make) in
		make.edges.equalTo(self).offset(UIEdgeInsetsZero)
		
		}
		
		gifImageView.snp_makeConstraints { (make) in
			make.trailing.equalTo(imageView)
			make.bottom.equalTo(imageView)
		}
	}
}












